//
//  BizhiViewController.m
//  A_HandHeldLOL
//
//  Created by Zhao YanBin on 16/1/21.
//  Copyright © 2016年 zhaoyanbin. All rights reserved.
//

#import "BizhiViewController.h"
#import "AFNModel.h"
#import "ZuixinbizhiModel.h"
#import <UIImageView+WebCache.h>
#import "ZuixinbizhiCollectionViewCell.h"
#import "MWE_layout.h"
#import "ZuireCollectionViewCell.h"
#import "ZuirebizhiModel.h"
#import "FenleiCollectionViewCell.h"
#import "FenleiModel.h"
#import "ZuixinXQViewController.h"
#import <MJRefresh.h>
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
@interface BizhiViewController ()<MWE_LayoutItemHeightDelegate, UIScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>
@property(nonatomic, retain)NSMutableArray *buttonArr;


//头部下划线scollview
@property(nonatomic, retain)UIScrollView *xianScollerView;
//下方大scrollview
@property(nonatomic, retain)UIScrollView *scrollerView;

//最新colletion
@property(nonatomic, retain)UICollectionView *colltion;
//最热collection
@property(nonatomic, retain)UICollectionView *zuirecolltion;
//分类collection
@property(nonatomic, retain)UICollectionView *fenleicolltion;

//收藏
@property(nonatomic, retain)UIView *shoucangVC;

@property(nonatomic, retain)NSMutableArray *zuixinArr;
@property(nonatomic, retain)NSMutableArray *zuireArr;
@property(nonatomic, retain)NSMutableArray *fenleiArr;

@property(nonatomic, assign)NSInteger number;
@end

@implementation BizhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"壁纸";
//    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self creattoubu];
    
    [self creatScrollerView];
    
    //最新
    [self creatzuixincollection];
    
    [self zuixinshuju];
    
    //最热
    [self creatzuirecollection];
    
    [self zuireshuju];
    
    //分类
    [self creatfenleicollection];
    
    [self fenleishuju];
    //收藏
//    [self shoucang];
    
    [self zuixinxiala];
    
    [self zuirexiala];
    
}
- (void)creattoubu
{
    NSArray *arrT = @[@"最新", @"最热", @"分类",@"收藏"];
    self.buttonArr = [NSMutableArray array];
    for (NSInteger i = 0; i < 4; i++) {
        //头部btn
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
        btn.frame = CGRectMake(i * WIDTH / 4, 64, WIDTH / 4  , 49);
        [btn setTitle:arrT[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        [self.buttonArr addObject:btn];
        if (i == 0) {
            [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        }
    }
    //btn下面的红线
    self.xianScollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 113, WIDTH  , 1)];
    _xianScollerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_xianScollerView];
    [_xianScollerView release];
    _xianScollerView.contentSize = CGSizeMake(WIDTH, 1);
    _xianScollerView.showsHorizontalScrollIndicator = NO;
    _xianScollerView.showsVerticalScrollIndicator = NO;
    _xianScollerView.bounces = NO;
    //scroll上得view
    UIView *qqq = [[UIView alloc] initWithFrame:CGRectMake(0, 64, WIDTH / 4, 1)];
    qqq.backgroundColor = [UIColor blueColor];
    [_xianScollerView addSubview:qqq];
    [qqq release];
    
    
}

//btn点击方法
- (void)btnAction:(UIButton *)btn
{
    [_scrollerView setContentOffset:CGPointMake(WIDTH * (btn.tag - 1), 0)animated:YES] ;
    [_xianScollerView setContentOffset:CGPointMake(- (WIDTH / 4) * (btn.tag - 1), 0)];
    for (UIButton *btn in _buttonArr) {
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
}
//创建ScrollerView
- (void)creatScrollerView
{
    self.scrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 114, WIDTH, HEIGHT - 50)];
    self.scrollerView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_scrollerView];
    [_scrollerView release];
    _scrollerView.delegate = self;
    
    //scroll内容大小
    _scrollerView.contentSize = CGSizeMake(WIDTH* 4, HEIGHT - 50);
    
    //整页翻动
    _scrollerView.pagingEnabled = YES;
    
    
    _scrollerView.showsHorizontalScrollIndicator = NO;
    _scrollerView.showsVerticalScrollIndicator = NO;
    _scrollerView.bounces = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _scrollerView) {
        [_xianScollerView setContentOffset:CGPointMake(- scrollView.contentOffset.x / 4, 0)animated:YES];
        NSInteger i = scrollView.contentOffset.x / WIDTH;
        for (UIButton *btn in _buttonArr) {
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [_buttonArr[i] setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    }
}


//创建最新collectionView
-(void)creatzuixincollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //cell的内容大小
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 30 )/ 2 , 300 );
    
    //上左下右
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //cell的间距
    layout.minimumLineSpacing = 10;
    
//    _colltion.contentInset = UIEdgeInsetsMake(144, 0, 0, 0);
    _colltion = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height- 49  - 49 - 64) collectionViewLayout:layout];
    //    _colltion.backgroundColor = [UIColor yellowColor];
    _colltion.delegate = self;
    _colltion.dataSource = self;
    _colltion.tag = 1000;
    
    //整页翻动
//    _colltion.pagingEnabled = YES;
    [self.scrollerView addSubview:_colltion];
    
    //注册重用池
    [_colltion registerClass:[ZuixinbizhiCollectionViewCell class] forCellWithReuseIdentifier:@"zuixin"];
    
    [layout release];
    [_colltion release];

}
//最新网络数据处理
-(void)zuixinshuju
{
    NSString *str = @"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=new&page=0&num=20&plat=ios&version=9660";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"wallpapers"];
        self.zuixinArr = [ZuixinbizhiModel modelHanderWithArray:arr];
        [self.colltion reloadData];
    }];
}

//上拉加载  下拉刷新
-(void)zuixinxiala
{
    self.number = 0;
    //上拉加载
    self.colltion.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(zuixinheader)];
    //结束刷新
//    [self.colltion.mj_header endRefreshing];
    
    //下拉刷新
    self.colltion.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(zuixinshangla)];
    
    if (_zuixinArr.count != 0) {
        //开始刷新

    }
}

//上拉加载
-(void)zuixinshangla
{
     [_colltion.mj_footer beginRefreshing];
    self.number += 1;
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=new&page=%ld&num=20&plat=ios&version=9660", self.number] completion:^(id result) {
        NSArray *arrTemp = [ZuixinbizhiModel modelHanderWithArray:result[@"wallpapers"]];
        for (ZuixinbizhiModel *m in arrTemp) {
            [self.zuixinArr addObject:m];
        }
        [_colltion reloadData];
    }];
    [_colltion.mj_footer endRefreshing];
}


//下拉刷新
-(void)zuixinheader
{
    NSString *str = @"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=new&page=0&num=20&plat=ios&version=9660";
    [AFNModel GETWithURL:str completion:^(id result) {
        self.zuixinArr = [NSMutableArray array];
        NSArray *arr = result[@"wallpapers"];
        self.zuixinArr = [ZuixinbizhiModel modelHanderWithArray:arr];
        
//        刷新时间
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新collectionview
            [self.colltion reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.colltion.mj_header endRefreshing];
        });
    }];
}

//最热网络数据处理
-(void)zuireshuju
{
    
    NSString *str = @"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=hot&page=0&num=20&plat=ios&version=9660";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"wallpapers"];
        self.zuireArr = [ZuirebizhiModel modelHanderWithArray:arr];
        [self.zuirecolltion reloadData];
    }];
    
}
//创建最热collection
-(void)creatzuirecollection
{
    MWE_layout *layout = [[MWE_layout alloc] init];
    layout.columCounts = 3;
    layout.columSparce = 10;
    layout.rowSparce = 10;
    layout.edgtInsets = UIEdgeInsetsMake(10, 10, 10, 10);
    _zuirecolltion = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width , 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64) collectionViewLayout:layout];
    _zuirecolltion.backgroundColor = [UIColor whiteColor];
    layout.delegate = self;
    _zuirecolltion.dataSource = self;
    _zuirecolltion.delegate = self;
    _zuirecolltion.tag = 2000;
    //整页翻动
//    _zuirecolltion.pagingEnabled = YES;
    [self.scrollerView addSubview:_zuirecolltion];
    
    //注册重用池
    [_zuirecolltion registerClass:[ZuireCollectionViewCell class] forCellWithReuseIdentifier:@"zuire"];
    
    [_zuirecolltion release];
    
}
//上拉加载  下拉刷新
-(void)zuirexiala
{
    self.number = 0;
    
    //下拉刷新
    self.zuirecolltion.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(zuireheader)];
    //结束刷新
//    [self.zuirecolltion.mj_header beginRefreshing];
    
    //上拉加载
    self.zuirecolltion.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(zuireshangla)];
    
    if (_zuireArr.count != 0) {
        //开始刷新
        
    }
}

//上拉加载
-(void)zuireshangla
{
    [_zuirecolltion.mj_footer beginRefreshing];
    self.number += 1;
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=hot&page=%ld&num=20&plat=ios&version=9660", self.number] completion:^(id result) {
        NSArray *arrTemp = [ZuirebizhiModel modelHanderWithArray:result[@"wallpapers"]];
        for (ZuirebizhiModel *m in arrTemp) {
            [self.zuireArr addObject:m];
        }
        [_zuirecolltion reloadData];
        [_zuirecolltion.mj_footer endRefreshing];
    }];
}


//下拉刷新
-(void)zuireheader
{
    NSString *str = @"http://qt.qq.com/php_cgi/lol_goods/varcache_wallpaper_list.php?type=hot&page=0&num=20&plat=ios&version=9660";
    [AFNModel GETWithURL:str completion:^(id result) {
        self.zuireArr = [NSMutableArray array];
        NSArray *arr = result[@"wallpapers"];
        self.zuireArr = [ZuirebizhiModel modelHanderWithArray:arr];
        
        //        刷新时间
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新collectionview
            [self.zuirecolltion reloadData];
            
            // 拿到当前的下拉刷新控件，结束刷新状态
            [self.zuirecolltion.mj_header endRefreshing];
//        });
    }];
}

//创建分类
-(void)creatfenleicollection
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    //cell的内容大小
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 40 )/ 3 , (self.view.frame.size.width - 40 )/ 3 );
    
    //上左下右
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    //cell的间距
    layout.minimumLineSpacing = 10;
    
    _fenleicolltion = [[UICollectionView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * 2, 0, self.view.frame.size.width, self.view.frame.size.height- 49  - 49 - 64) collectionViewLayout:layout];
    _fenleicolltion.delegate = self;
    _fenleicolltion.dataSource = self;
    _fenleicolltion.tag = 3000;
    
    //整页翻动
    _fenleicolltion.pagingEnabled = YES;
    [self.scrollerView addSubview:_fenleicolltion];
    
    //注册重用池
    [_fenleicolltion registerClass:[FenleiCollectionViewCell class] forCellWithReuseIdentifier:@"fenlei"];
    [layout release];
    [_fenleicolltion release];
    
}

//分类网络请求
-(void)fenleishuju
{
    
    NSString *str = @"http://qt.qq.com/php_cgi/lol_goods/varcache_kind_index.php?page=0&num=20&plat=ios&version=9660";
    [AFNModel GETWithURL:str completion:^(id result) {
        NSArray *arr = result[@"categories"];
        self.fenleiArr = [FenleiModel modelHanderWithArray:arr];
        [self.fenleicolltion reloadData];
    }];
    
}

//收藏
-(void)shoucang
{
    self.shoucangVC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    _shoucangVC.backgroundColor = [UIColor yellowColor];
    [self.scrollerView addSubview:_shoucangVC];
    [_shoucangVC release];
}



//collection两个方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView == _colltion) {
          return _zuixinArr.count;
    }else if(collectionView == _zuirecolltion){
        return _zuireArr.count;
    }else{
        return _fenleiArr.count;
    }
  
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _colltion) {
    ZuixinbizhiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zuixin" forIndexPath:indexPath];
    
    ZuixinbizhiModel *m = _zuixinArr[indexPath.item];
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.url]];
    return cell;
    }
    else if(collectionView == _zuirecolltion){
        ZuireCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"zuire" forIndexPath:indexPath];
        ZuirebizhiModel *m = _zuireArr[indexPath.item];
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.url]];
        return cell;
    }
    else if(collectionView == _fenleicolltion){
        FenleiCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fenlei" forIndexPath:indexPath];
        FenleiModel *m = _fenleiArr[indexPath.item];
        cell.name.text = m.name;
        [cell.img sd_setImageWithURL:[NSURL URLWithString:m.thumbUrl]];
        return cell;
    }
    return NULL;
}

//collectionView点击
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView == _colltion) {
        ZuixinXQViewController *zxXQ = [[ZuixinXQViewController alloc] init];
        zxXQ.zxid = [_zuixinArr[indexPath.item] Id];
        [self.navigationController pushViewController:zxXQ animated:YES];
    }
}



//高度
- (CGFloat)layout:(MWE_layout *)layout heightForItemAtIndexPath:(NSIndexPath *)indexPath width:(CGFloat)width
{
    //tw/th = iw/ih
    ZuirebizhiModel *m = _zuireArr[indexPath.item];
    CGFloat h = width *m.thumb_height.doubleValue / m.thumb_width.doubleValue;
    return h;
}

//分类collectionVC


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
